package project1.dao;

import com.alibaba.druid.util.StringUtils;
import org.apache.commons.dbutils.QueryRunner;

import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import project1.model.Admin;

import project1.utils.DruidUtils;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AdminDaoIml implements AdminDao {
    @Override
    public Admin   login(Admin admin) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        Admin query = null;
        System.out.println(admin.getEmail()+admin.getPwd());
        try {

            query = runner.query("select * from admin where email = ? and pwd = ?",
                    new BeanHandler<Admin>(Admin.class),
                    admin.getEmail(),
                    admin.getPwd());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        //System.out.println("数据库中找到了"+query);
        return query;
    }

    @Override
    public List<Admin> allAdmins() {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        List<Admin> admins = null;
        try {
            admins = runner.query("select * from admin", new BeanListHandler<Admin>(Admin.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return admins;
    }

    /**
     * 1.当账号搜索栏有数据：select * from admin where email like?
     * 2.当昵称搜索栏有数据：select * from admin where nickname like？
     * 3.当账号，昵称都有数据：select * from admin where email like and nickname like？
     * 动态sql的思想
     * @param admin
     * @return
     */
    @Override
    public List<Admin> getSearchAdmins(Admin admin) {
        Map<String,Object> result = getDynamicSql(admin);
        String sql = (String) result.get("sql");
        List<String> params =(List<String>) result.get("params");
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        List<Admin> admins = null;
        try {
            admins = runner.query(sql, new BeanListHandler<Admin>(Admin.class), params.toArray());
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return admins;
    }

    private Map<String, Object> getDynamicSql(Admin admin) {
        String  base = "select * from admin where 1 =1 ";
        List<String> params = new ArrayList<String>();
        if (!StringUtils.isEmpty((admin.getEmail()))){
         base = base + "and email like ?";
            System.out.println("base:" + base);
         params.add("%" + admin.getEmail() + "%");

        }
        if (!StringUtils.isEmpty((admin.getNickname()))){
            base = base + "and nickname like ?";
            System.out.println("base:" + base);
            params.add("%" + admin.getNickname() + "&" );
        }
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("sql",base);
        map.put("params",params);
        return  map;
    }

    @Override
    public void deleteAdmin( String id) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        try {
           runner.update("delete from admin where id = ? " , id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void addAdmin(Admin admin) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        int id = 0;
        try {
             id =  runner.execute("select count(*) from admin",Integer.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        admin.setId(id);
        try {
             runner.update("insert into admin values (?,?,?,?)",
                    admin.getId(),
                    admin.getEmail(),
                    admin.getNickname(),
                    admin.getPwd());

        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    @Override
    public Admin getAdminInfo(String id) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        Admin admins =null;
        try {
            admins = runner.query("select * from admin where id = ?", new BeanHandler<Admin>(Admin.class),id );
        } catch (SQLException e) {
            e.printStackTrace();
        }
       return admins;
    }

    @Override
    public void updateAdmin(Admin admin) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        try {
            runner.update("update admin set email = ? , nickname = ? , pwd = ? where id = ?",
                    admin.getEmail(),
                    admin.getNickname(),
                    admin.getPwd(),
                    admin.getId());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
